/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package wordcounts;

/**
 *
 * @author Administrator
 */
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;

public class words {

//	 public static int bytenumber (String filename){
//		      int wordcount = 0;
//		   	int linecount = 0;
//		   	List<String> text2= new ArrayList<String>();
//		   	List<String> text= new ArrayList<String>();
//		 try {
//				FileInputStream in = new FileInputStream(filename);
//				InputStreamReader inReader = new InputStreamReader(in);
//				BufferedReader bufReader = new BufferedReader(inReader);
//				String line = null;
//				
//				while((line = bufReader.readLine()) != null){
//				 	char ch[]=new char[line.length()];
//			    	line.getChars(0,line.length()-1,ch,0);
//				for(int i=0;i<=line.length()-1;i++)
//				{
//				 	if(Character.isUpperCase(ch[i]))
//				    	Character.toLowerCase(ch[i]);
//					
//				}
//				text.add(line);
//				
//				wordcount+=line.length();
//				linecount ++;
//				}
//				
//				bufReader.close();
//				inReader.close();
//				in.close();
//			} catch (Exception e) {
//				e.printStackTrace();
//				System.out.println("读取" +filename+ "出错！");
//			}
//			//转为小写
//			for(String words:text)
//			{
//				String[] word = words.split("[^a-zA-Z0-9]"); 
//				
//				for(String pp:word)
//				{
//					 String regex ="^[a-z]{4}[a-z0-9\\s]*$";
//					 if(pp.matches(regex)==true){
//						 text2.add(pp);
//					 }
//				}
//			}
//			return wordcount;
//		 
//	 }
//	  public static int wordnumber(String filename ) {
//		    int wordcount = 0;
//		   	int linecount = 0;
//		  List<String> text2= new ArrayList<String>();
//		   	List<String> text= new ArrayList<String>();
//		  try {
//				FileInputStream in = new FileInputStream(filename);
//				InputStreamReader inReader = new InputStreamReader(in);
//				BufferedReader bufReader = new BufferedReader(inReader);
//				String line = null;
//				
//				
//				while((line = bufReader.readLine()) != null){
////				 	char ch[]=new char[line.length()];
////			    	line.getChars(0,line.length()-1,ch,0);
//				for(int i=0;i<=line.length()-1;i++)
//				{
//				 	if(Character.isUpperCase(line.charAt(i))) //对大写字母的判断
//				    	Character.toLowerCase(line.charAt(i));
//				 	
//				 	
//					
//				}
//				wordcount+=line.length();//字符数统计
//				linecount ++;//行数统计
//				text.add(line);
//				
//				
//				}
//				
//				bufReader.close();
//				inReader.close();
//				in.close();
//			} catch (Exception e) {
//				e.printStackTrace();
//				System.out.println("读取" +filename+ "出错！");
//			}
//			//转为小写
//			for(String words:text)
//			{
//				String[] word = words.split("[^a-zA-Z0-9]"); 
//				
//				for(String pp:word)
//				{
//					 String regex ="^[a-z]{4}[a-z0-9\\s]*$";
//					 if(pp.matches(regex)==true){
//						 text2.add(pp);
//					 }
//				}
//			}
//          return text2.size();
//      }
//	  public static List<Map.Entry<String, Integer>> out(String filename ) {
//		  List<String> text2= new ArrayList<String>();
//		   	List<String> text= new ArrayList<String>();
//		  try {	
//		  FileInputStream in = new FileInputStream(filename);
//			InputStreamReader inReader = new InputStreamReader(in);
//			BufferedReader bufReader = new BufferedReader(inReader);
//			String line = null;
//			
//			
//			while((line = bufReader.readLine()) != null){
//			 	char ch[]=new char[line.length()];
//		    	line.getChars(0,line.length()-1,ch,0);
//			for(int i=0;i<=line.length()-1;i++)
//			{
//			 	if(Character.isUpperCase(ch[i]))
//			    	Character.toLowerCase(ch[i]);
//				
//			}
//			text.add(String.valueOf(ch));
//			
//			
//			
//			}
//			
//			bufReader.close();
//			inReader.close();
//			in.close();
//		} catch (Exception e) {
//			e.printStackTrace();
//			System.out.println("读取" +filename+ "出错！");
//		}
//		//转为小写
//		for(String words:text)
//		{
//			String[] word = words.split("[^a-zA-Z0-9]"); 
//			
//			for(String pp:word)
//			{
//				 String regex ="^[a-z]{4}[a-z0-9]*$";
//				 if(pp.matches(regex)==true){
//					 text2.add(pp);
//				 }
//			}
//		}
//		  Map<String, Integer> map = new TreeMap<String, Integer>();
//			for(String word:text2)
//			 {if(map.get(word) != null){ 
//	                map.put(word,map.get(word) + 1);  
//	            }else{  
//	                map.put(word,1);  
//	            }  
//	  
//			 }
//			
//			 List<Map.Entry<String, Integer>> infoIds = new ArrayList<Map.Entry<String, Integer>>(  
//		             map.entrySet());  
//			 List<Map.Entry<String, Integer>> infoIds1 = new ArrayList<Map.Entry<String, Integer>>(  
//		             map.entrySet());  
//		     Collections.sort(infoIds, new Comparator<Map.Entry<String, Integer>>() {  
//		         public int compare(Map.Entry<String, Integer> o1,  
//		                 Map.Entry<String, Integer> o2) {  
//		             return ( o2.getValue()-o1.getValue());  
//		         }  
//		     });  
//		     System.out.println("--------------排序后--------------");  
//		     for (int i = 0; i < infoIds.size(); i++) {  
//		    	 Entry<String,Integer> ent=infoIds.get(i);  
//		    	 if(i<=9){
//		    		 infoIds1.add(ent);
//		         System.out.println(ent.getKey()+":"+ent.getValue());
//		    	 }
//		       
//	}
//			return infoIds1;
//      }
    public static int line(String filename) {
        int wordcount = 0;
        int linecount = 0;
        List<String> text2 = new ArrayList<String>();
        List<String> text = new ArrayList<String>();
        try {
            FileInputStream in = new FileInputStream(filename);
            InputStreamReader inReader = new InputStreamReader(in);
            BufferedReader bufReader = new BufferedReader(inReader);
            String line = null;

            while ((line = bufReader.readLine()) != null) {
                for (int i = 0; i <= line.length() - 1; i++) {
                    if (Character.isUpperCase(line.charAt(i))) //对大写字母的判断
                    {
                        Character.toLowerCase(line.charAt(i));
                    }

                }
                wordcount += line.length();//字符数统计
                linecount++;//行数统计
                text.add(line);

            }

            bufReader.close();
            inReader.close();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("读取" + filename + "出错！");
        }
        //转为小写
        for (String words : text) {
            String[] word = words.split("[^a-zA-Z0-9]");

            for (String pp : word) {
                String regex = "^[a-z]{4}[a-z0-9\\s]*$";
                if (pp.matches(regex) == true) {
                    text2.add(pp);
                }
            }
        }
        return linecount;

    }

    public static int bytenumber(String filename) {
        int wordcount = 0;
        int linecount = 0;
        List<String> text2 = new ArrayList<String>();
        List<String> text = new ArrayList<String>();
        try {
            FileInputStream in = new FileInputStream(filename);
            InputStreamReader inReader = new InputStreamReader(in);
            BufferedReader bufReader = new BufferedReader(inReader);
            String line = null;

            while ((line = bufReader.readLine()) != null) {
//			 	char ch[]=new char[line.length()];
//		    	line.getChars(0,line.length()-1,ch,0);
                for (int i = 0; i <= line.length() - 1; i++) {
                    if (Character.isUpperCase(line.charAt(i))) //对大写字母的判断
                    {
                        Character.toLowerCase(line.charAt(i));
                    }

                }
                wordcount += line.length();//字符数统计
                linecount++;//行数统计
                text.add(line);

            }

            bufReader.close();
            inReader.close();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("读取" + filename + "出错！");
        }
        //转为小写
        for (String words : text) {
            String[] word = words.split("[^a-zA-Z0-9]");

            for (String pp : word) {
                String regex = "^[a-z]{4}[a-z0-9\\s]*$";
                if (pp.matches(regex) == true) {
                    text2.add(pp);
                }
            }
        }
        return wordcount;

    }

    public static int wordnumber(String filename) {
        int wordcount = 0;
        int linecount = 0;
        List<String> text2 = new ArrayList<String>();
        List<String> text = new ArrayList<String>();
        try {
            FileInputStream in = new FileInputStream(filename);
            InputStreamReader inReader = new InputStreamReader(in);
            BufferedReader bufReader = new BufferedReader(inReader);
            String line = null;

            while ((line = bufReader.readLine()) != null) {
//			 	char ch[]=new char[line.length()];
//		    	line.getChars(0,line.length()-1,ch,0);
                for (int i = 0; i <= line.length() - 1; i++) {
                    if (Character.isUpperCase(line.charAt(i))) //对大写字母的判断
                    {
                        Character.toLowerCase(line.charAt(i));
                    }

                }
                wordcount += line.length();//字符数统计
                linecount++;//行数统计
                text.add(line);

            }

            bufReader.close();
            inReader.close();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("读取" + filename + "出错！");
        }
        //转为小写
        for (String words : text) {
            String[] word = words.split("[^a-zA-Z0-9]");

            for (String pp : word) {
                String regex = "^[a-z]{4}[a-z0-9\\s]*$";
                if (pp.matches(regex) == true) {
                    text2.add(pp);
                }
            }
        }
        return text2.size();
    }

    public static List<Map.Entry<String, Integer>> out(String filename) {
        List<String> text2 = new ArrayList<String>();
        List<String> text = new ArrayList<String>();
        try {
            FileInputStream in = new FileInputStream(filename);
            InputStreamReader inReader = new InputStreamReader(in);
            BufferedReader bufReader = new BufferedReader(inReader);
            String line = null;

            while ((line = bufReader.readLine()) != null) {
//		 	char ch[]=new char[line.length()];
//	    	line.getChars(0,line.length()-1,ch,0);
                for (int i = 0; i <= line.length() - 1; i++) {
                    if (Character.isUpperCase(line.charAt(i))) //对大写字母的判断
                    {
                        Character.toLowerCase(line.charAt(i));
                    }

                }

                text.add(line);

            }

            bufReader.close();
            inReader.close();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("读取" + filename + "出错！");
        }
        //转为小写
        for (String words : text) {
            String[] word = words.split("[^a-zA-Z0-9]");

            for (String pp : word) {
                String regex = "^[a-z]{4}[a-z0-9]*$";
                if (pp.matches(regex) == true) {
                    text2.add(pp);
                }
            }
        }
        Map<String, Integer> map = new TreeMap<String, Integer>();
        for (String word : text2) {
            if (map.get(word) != null) {
                map.put(word, map.get(word) + 1);
            } else {
                map.put(word, 1);
            }

        }

        List<Map.Entry<String, Integer>> infoIds = new ArrayList<Map.Entry<String, Integer>>(
                map.entrySet());
        List<Map.Entry<String, Integer>> infoIds1 = new ArrayList<Map.Entry<String, Integer>>();
        Collections.sort(infoIds, new Comparator<Map.Entry<String, Integer>>() {
            public int compare(Map.Entry<String, Integer> o1,
                    Map.Entry<String, Integer> o2) {
                return (o2.getValue() - o1.getValue());
            }
        });
        System.out.println("--------------排序后--------------");
        for (int i = 0; i < infoIds.size(); i++) {
            Entry<String, Integer> ent = infoIds.get(i);
            if (i <= 9) {
                infoIds1.add(ent);

            }

        }
        return infoIds1;
    }

    public static List<Map.Entry<String, Integer>> out(String filename, int h) {
        List<String> text2 = new ArrayList<String>();
        List<String> text = new ArrayList<String>();
        try {
            FileInputStream in = new FileInputStream(filename);
            InputStreamReader inReader = new InputStreamReader(in);
            BufferedReader bufReader = new BufferedReader(inReader);
            String line = null;

            while ((line = bufReader.readLine()) != null) {
//		 	char ch[]=new char[line.length()];
//	    	line.getChars(0,line.length()-1,ch,0);
                for (int i = 0; i <= line.length() - 1; i++) {
                    if (Character.isUpperCase(line.charAt(i))) //对大写字母的判断
                    {
                        Character.toLowerCase(line.charAt(i));
                    }

                }

                text.add(line);

            }

            bufReader.close();
            inReader.close();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("读取" + filename + "出错！");
        }
        //转为小写
        for (String words : text) {
            String[] word = words.split("[^a-zA-Z0-9]");

            for (String pp : word) {
                String regex = "^[a-z]{4}[a-z0-9]*$";
                if (pp.matches(regex) == true) {
                    text2.add(pp);
                }
            }
        }
        Map<String, Integer> map = new TreeMap<String, Integer>();
        for (String word : text2) {
            if (map.get(word) != null) {
                map.put(word, map.get(word) + 1);
            } else {
                map.put(word, 1);
            }

        }

        List<Map.Entry<String, Integer>> infoIds = new ArrayList<Map.Entry<String, Integer>>(
                map.entrySet());
        List<Map.Entry<String, Integer>> infoIds1 = new ArrayList<Map.Entry<String, Integer>>();
        Collections.sort(infoIds, new Comparator<Map.Entry<String, Integer>>() {
            public int compare(Map.Entry<String, Integer> o1,
                    Map.Entry<String, Integer> o2) {
                return (o2.getValue() - o1.getValue());
            }
        });
        System.out.println("--------------排序后--------------");
        for (int i = 0; i < infoIds.size(); i++) {
            Entry<String, Integer> ent = infoIds.get(i);
            if (i < h) {
                infoIds1.add(ent);

            }

        }
        return infoIds1;
    }

    public static List<Map.Entry<String, Integer>> outg(String filename, int k) {
        List<String> text2 = new ArrayList<String>();
        List<String> text = new ArrayList<String>();
        List<String> text3 = new ArrayList<String>();
        try {
            FileInputStream in = new FileInputStream(filename);
            InputStreamReader inReader = new InputStreamReader(in);
            BufferedReader bufReader = new BufferedReader(inReader);
            String line = null;

            while ((line = bufReader.readLine()) != null) {
//		 	char ch[]=new char[line.length()];
//	    	line.getChars(0,line.length()-1,ch,0);
                for (int i = 0; i <= line.length() - 1; i++) {
                    if (Character.isUpperCase(line.charAt(i))) //对大写字母的判断
                    {
                        Character.toLowerCase(line.charAt(i));
                    }

                }

                text.add(line);

            }

            bufReader.close();
            inReader.close();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("读取" + filename + "出错！");
        }
        //转为小写
        for (String words : text) {
            String[] word = words.split("[^a-zA-Z0-9]");

            for (String pp : word) {
                String regex = "^[a-z]{4}[a-z0-9]*$";
                if (pp.matches(regex) == true) {
                    text2.add(pp);
                }
            }
        }
        for (int h = 0; h < text2.size() - k + 1; h++) {
            String group = "";
            for (int y = 0; y < k; y++) {
                if (y == 0) {
                    group = group + text2.get(h);
                } else {
                    group = group + " " + text2.get(h + y);
                }
            }

            text3.add(group);
        }
        Map<String, Integer> map = new TreeMap<String, Integer>();
        for (String word : text3) {
            if (map.get(word) != null) {
                map.put(word, map.get(word) + 1);
            } else {
                map.put(word, 1);
            }

        }

        List<Map.Entry<String, Integer>> infoIds = new ArrayList<Map.Entry<String, Integer>>(
                map.entrySet());
//		 List<Map.Entry<String, Integer>> infoIds1 = new ArrayList<Map.Entry<String, Integer>>( );  
//	     Collections.sort(infoIds, new Comparator<Map.Entry<String, Integer>>() {  
//	         public int compare(Map.Entry<String, Integer> o1,  
//	                 Map.Entry<String, Integer> o2) {  
//	             return ( o2.getValue()-o1.getValue());  
//	         }  
//	     });  
//	     System.out.println("--------------排序后--------------");  
//	     for (int i = 0; i < infoIds.size(); i++) {  
//	    	 Entry<String,Integer> ent=infoIds.get(i);  
//	    	 if(i<=10){
//	    		 infoIds1.add(ent);
//	        
//	    	 }
//	       
//}
        return infoIds;
    }

}
